package piwan;


public class Vector2 {
	public double x, y;
	
	public Vector2( double ix, double iy ) { x = ix; y = iy; }
	public Vector2() { x = y = 0.0; }
	
	public Vector2	set(Vector2 v) { x=v.x;y=v.y; return this; }
	public Vector2	add( Vector2 v ) { x+=v.x; y+=v.y; return this; }
	public Vector2	sub( Vector2 v ) { x-=v.x; y-=v.y; return this; }
	public Vector2	mul( double s ) { x*=s; y*=s; return this; }
	public Vector2	plus( Vector2 v ) { return new Vector2(x+v.x,y+v.y); }
	public Vector2	minus( Vector2 v ) { return new Vector2(x-v.x,y-v.y); }
	public Vector2	times( double s ) { return new Vector2(x*s,y*s); }
	
	public double	dist2(Vector2 v) { return minus(v).norm2(); }
	public double	dist(Vector2 v) { return Math.sqrt(dist2(v)); }
	public boolean	closerThan(Vector2 v, double thr) { return dist2(v)<=thr*thr; }
	public double	dot( Vector2 v ) { return x*v.x + y*v.y; }
	public double	norm2() { return dot(this); }
	public double	norm() { return Math.sqrt(norm2()); }
	public Vector2	set( double newX, double newY ) { x = newX; y = newY; return this; }
	
	public Vector2 clone() { return new Vector2(x,y); }
	
	public String toString() { return "Vector2(" + x + ", " + y + ")"; }
	
	public Vector2	unit() {
		double n = norm();
		if( n == 0.0 )
			return new Vector2(0.0,0.0);
		else
			return new Vector2(x/n,y/n);
	}
}
